package com.acheli.utils.rotate;

import com.acheli.rideable.entites.ComponentEntity;
import com.acheli.rideable.functionLathes.ComponentFL;
import com.acheli.rideable.functionLathes.component.SeatFL;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import org.joml.Quaternionf;
import org.joml.Vector3f;

public class RotateWith {

    public static Quaternionf copy(Quaternionf rotation) {
        Quaternionf ready = new Quaternionf();
        ready.x = rotation.x;
        ready.y = rotation.y;
        ready.z = rotation.z;
        ready.w = rotation.w;
        return ready;
    }

    /**
     * 取反四元数的 Y 轴旋转
     * @param rotation 原始四元数
     * @return 取反 Y 轴旋转后的四元数
     */
    public static Quaternionf translateRotation(Quaternionf rotation) {
        Quaternionf ready = new Quaternionf();
        ready.x = rotation.z;
        ready.y = rotation.y;
        ready.z = -rotation.x;
        ready.w = rotation.w;
        return ready;
    }

    public static Quaternionf translateForRiseRotation(Quaternionf rotation) {
        Quaternionf ready = new Quaternionf();
        ready.x = rotation.z;
        ready.y = -rotation.y;
        ready.z = rotation.x;
        ready.w = rotation.w;
        return ready;
    }


    /**
     * 使用四元数旋转坐标点
     * @param x 原坐标X
     * @param y 原坐标Y
     * @param z 原坐标Z
     * @param rotation 旋转四元数（需为单位四元数）
     * @return 旋转后的坐标
     */
    public static Vec3 rotatePoint(double x, double y, double z, Quaternionf rotation) {
        // 1. 将坐标转换为三维向量
        Vector3f point = new Vector3f((float) x, (float) y, (float) z);

        // 2. 应用四元数旋转
        Quaternionf rY = translateRotation(rotation);
        rY.transform(point);

        // 3. 返回旋转后的坐标
        return new Vec3(point.x, point.y, point.z);
    }

    public static void rotateEntity(Entity entity, Quaternionf rotation) {
        // 使用getEulerAnglesXYZ方法获取旋转角度
        Vec3 forward = EntityDirectionUtils.getForwardDirection(translateRotation(rotation));
        // 使用Math.atan2计算弧度
        double angleRadians = Math.atan2(forward.x(), forward.z());
        // 将弧度转换为度
        double forwardYaw = -Math.toDegrees(angleRadians);
        double forwardPitch = -Math.toDegrees(forward.y);
        if (entity instanceof Player player) {
            if (player.getVehicle() instanceof ComponentEntity componentEntity) {
                ComponentFL componentFL = componentEntity.getComponentFL();
                if (componentFL instanceof SeatFL seatFL) {
                    seatFL.setPlayerRotate(player, forwardYaw, forwardPitch);
                }

            }
        }

    }

    // 示例用法
    public static void main(String[] args) {
        // 定义旋转四元数（绕Y轴旋转90度）
        Quaternionf rotation = new Quaternionf()
                .rotateY((float) Math.toRadians(90));

        // 原始坐标点
        Vec3 originalPoint = new Vec3(1, 0, 0);

        // 计算旋转后的坐标
        Vec3 rotatedPoint = rotatePoint(
                originalPoint.x, originalPoint.y, originalPoint.z,
                rotation
        );

        // 输出结果（应接近 (0, 0, 1)）
        System.out.println("旋转后的坐标: " + rotatedPoint);
    }
}